package com.hjwoo.app.views;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.been.RegisterResult;
import com.hjwoo.app.been.UserAccessTokenOBJ;
import com.hjwoo.app.common.AppManager;
import com.hjwoo.app.common.LoanApplication;
import com.hjwoo.app.fragment.UserCenterFragment;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

import java.util.Timer;
import java.util.TimerTask;

public class RegisterActivity02 extends BaseAppCompatActivity implements RequestListener
{

    public static final  int    REQUEST_CODE = 1;
    private static final String TAG          = RegisterActivity02.class.getSimpleName();
    Button   regist02_commit;
    EditText regist02_name, regist02_pwd, regist02_code;
    TextView  regist02_city;
    // 计时任务
    TimerTask task;
    Gson gson = new Gson();

    // 获取验证码
    TextView obtain_tv;
    String   phonenumber;
    private Timer timer = new Timer();
    // 短信倒计时时间
    private int   time  = 10;
    private String a;

    private CheckBox vispassword;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        //先判断是哪个页面返回过来的
        switch (resultCode)
        {
        case RESULT_OK:
//                    Bundle b=data.getExtras();
//                    String cityName = b.getString(SelectListActivity.CITYNAME);
            String cityName = data.getStringExtra("itemName");
            regist02_city.setText(cityName);
            a = cityName;
            break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout2);

        if (getIntent() != null)
        {
            phonenumber = getIntent().getStringExtra("phonenumber");
        }

        Utils.initTitle("注册", RegisterActivity02.this, true);

        regist02_name = (EditText) findViewById(R.id.regist02_name);//姓名
        regist02_city = (TextView) findViewById(R.id.regist02_city);//城市
        a = regist02_city.getText().toString().trim();
        regist02_pwd = (EditText) findViewById(R.id.regist02_pwd);//密码
        regist02_code = (EditText) findViewById(R.id.regist02_code);//验证码
        regist02_commit = (Button) findViewById(R.id.regist02_commit);

        vispassword = (CheckBox) findViewById(R.id.regist02_switchpwd);
        vispassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @TargetApi(Build.VERSION_CODES.CUPCAKE)
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b)
            {
                if (b)
                {
                    regist02_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    //                            将光标定义到最后面
                    regist02_pwd.setSelection(regist02_pwd.getText().length());
                }
                else
                {
                    regist02_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    //                            将光标定义到最后面
                    regist02_pwd.setSelection(regist02_pwd.getText().length());
                }
            }
        });


        findViewById(R.id.regist02_city_ll).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Bundle b = new Bundle();
                b.putSerializable("ls", new String[]{
                    "北京", "上海", "成都", "青岛", "济南", "郑州", "南京", "西安"
                });
                Intent i = new Intent(RegisterActivity02.this, SelectListActivity.class);
                i.putExtras(b);
                i.putExtra("equalStr", a);
                startActivityForResult(i, REQUEST_CODE);
            }
        });
        regist02_commit.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View view)
            {
                if (!Utils.isPasswordValid(regist02_pwd.getText().toString()))
                {
                    LoanToast.showToastShort("密码格式有误", RegisterActivity02.this);
                }
                else
                {
                    if (TextUtils.isEmpty(regist02_name.getText().toString()))
                    {
                        LoanToast.showToastShort("请输入您的姓名!", RegisterActivity02.this);
                        return;
                    }
                    if (TextUtils.isEmpty(regist02_city.getText().toString()))
                    {
                        LoanToast.showToastShort("请输入您所在的城市!", RegisterActivity02.this);
                        return;
                    }
                    if (TextUtils.isEmpty(regist02_pwd.getText().toString()))
                    {
                        LoanToast.showToastShort("请输入您的密码!", RegisterActivity02.this);
                        return;
                    }
                    if (TextUtils.isEmpty(regist02_code.getText().toString()))
                    {
                        LoanToast.showToastShort("请输入您的邀请码!", RegisterActivity02.this);
                        return;
                    }
                    doRegisterForPost();
                }
//                UserAccessTokenOBJ userAccessTokenOBJ = new UserAccessTokenOBJ();
//                userAccessTokenOBJ.setAppSessionId("111");

                //将id保存
//                LoanAccessTokenIdKeeper.writeAccessToken(RegisterActivity02.this,userAccessTokenOBJ);
//                setResult(UserCenterFragment.REGISTER_RESULT_CODE, new Intent().putExtra("loginFlag", true));
                // 关闭自身页面
//                finish();
            }
        });

    }

    /**
     * 注册
     */
    private void doRegisterForPost()
    {
        String password = regist02_pwd.getText().toString();
        RequestMethod.getMethod().doRegisterMsg(regist02_name.getText().toString(), regist02_city.getText().toString(), phonenumber, password, regist02_code.getText().toString(), RegisterActivity02.this, RegisterActivity02.this);
    }

    @Override
    public void requestSuccess(String tag, String message)
    {

        if (tag.equals(RequestTag.APPREGISTER))
        {
            RegisterResult rr = gson.fromJson(message, RegisterResult.class);
            LoanToast.showToastShort("注册成功", RegisterActivity02.this);
            UserAccessTokenOBJ userAccessTokenOBJ = new UserAccessTokenOBJ();
            userAccessTokenOBJ.setAppSessionId(rr.getAppSessionId());
            //将id保存
            LoanAccessTokenIdKeeper.writeAccessToken(RegisterActivity02.this, userAccessTokenOBJ);
            Message msg = new Message();
            msg.what = UserCenterFragment.USER_ISLOGIN;
            //获取用户handler
            UserCenterFragment.MyUserCenterHandler myUserCenterHandler = (UserCenterFragment.MyUserCenterHandler) LoanApplication.getInstance().getHandler();
            myUserCenterHandler.sendMessage(msg);

            //登录成功 将gragment切换到首页
            MainActivity.currIndex = 0;
            MainActivity.showFragment();
            MainActivity.group.check(R.id.foot_bar_home);
            // 关闭之前开启的所有页面
            AppManager.getAppManager().finishAllActivity();

        }
    }

    @Override
    public void requestError(String message)
    {
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), RegisterActivity02.this);
    }
}
